import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/db";

export const GET = async (request: NextRequest, response: any) => {
  //解析传过来的body参数

  const { searchParams } = new URL(request.url);
  const username = searchParams.get("username");
  let where = {};
  if (username) {
    where = {
      username: {
        contains: username, // 使用 contains 来进行模糊查询
      },
    };
  }
  const users = await prisma.user.findMany({
    where,
  });
  // 构建查询条件
  return NextResponse.json({
    success: true,
    data: users,
    msg: "success",
  });
};


export const POST = async (request: NextRequest, response: any) => {
  // const { body } = request;
    const body = await request.json();
  const { username, password } = body;
  console.log('body', body)
  const user = await prisma.user.create({
    data: {
      username,
      password,
    },
  });
  return NextResponse.json({
    success: true,
    data: user,
    msg: "success",
})
}
//根据id删除
export const DELETE = async (request: NextRequest, response: any) => {
  // const { id } = request.query;
    const { searchParams } = new URL(request.url);
  let id = searchParams.get("id");
  id = Number(id);
  const user = await prisma.user.delete({
    where: {
      id: id,
    },
  });
  return NextResponse.json({
    success: true,
    data: user,
    msg: "success",
  });
};